package com.acme.bankapp.domain.bank;

import com.acme.bankapp.service.bank.*;

public class CheckingAccount extends AbstractAccount {

    private double overdraft;

    public CheckingAccount(double balance, double overdraft) {
        if (balance < 0 || overdraft < 0) {
            throw new IllegalArgumentException();
        }
        this.balance = balance;
        this.overdraft = overdraft;
    }

    public void withdraw(double x) throws NotEnoughFundsException {

        if (this.balance >= x) {
            this.balance -= x;
            return;
        } else {
            if (this.balance + this.overdraft > x) {
                this.overdraft += this.balance - x;
                this.balance = 0.0;
                return;
            }
        }
        throw new OverDraftLimitExceededException("Max credit: " + maximumAmountToWithdraw());
    }

    public double maximumAmountToWithdraw() {

        return this.balance + this.overdraft;
    }

    public double getOverdraft() {
        return this.overdraft;
    }

    @Override
    public String toString() {
        return String.format("Checking account: %.2f;\nMax amount to withdraw: %.2f \nInteger value of balance: %d\n", getBalance(), maximumAmountToWithdraw(), decimalValue());
    }

}